package com.highspeed.bdk.controller.physical;


import com.bianmaba.spring.data.jpa.domain.PageableRequest;
import com.bianmaba.spring.data.jpa.domain.PageableResult;
import com.bianmaba.spring.data.jpa.domain.Result;
import com.bianmaba.spring.mvc.method.support.annotations.EntityParam;
import com.bianmaba.spring.mvc.method.support.annotations.JSON;
import com.bianmaba.spring.mvc.method.support.annotations.MapParam;
import com.highspeed.bdk.bean.entity.physical.*;
import com.highspeed.bdk.controller.base.BdkCrudController;
import com.highspeed.bdk.service.base.IBdkBaseService;
import com.highspeed.bdk.service.physical.IPhysicalTopologyService;
import io.swagger.annotations.*;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "physical_topology", method = RequestMethod.POST)
@Api(tags = "物理拓扑")
public class PhysicalTopologyController extends BdkCrudController<PhysicalTopology, Integer> {
    @Autowired
    private IPhysicalTopologyService physicalTopologyService;

    @Override
    protected IBdkBaseService<PhysicalTopology, Integer> getService() {
        return physicalTopologyService;
    }

    @JSON(excludeProperties = "data.*\\.machineBoxes")
    public PageableResult query(@MapParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        return super.query(params, pageable);
    }

    @ApiOperation(value = "保存整个物理拓扑图", notes = "参数为JSON数据;<br/>" +
            "数据中的链路信息不会被保存,需要单独调用physical_link\\save来保存;")
    @JSON
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Override
    public Result save(@RequestBody @ApiParam(name = "entity") PhysicalTopology entity) {
        return super.save(entity);
    }

    @ApiOperation(value = "根据物理拓扑ID查询虚线信息")
    @ApiImplicitParam(name = "id", value = "物理拓扑ID", dataType = "int", paramType = "query")
    @RequestMapping({"/get_virtual_links_by_physical_topology_id"})
    @JSON
    public List<Map<String, Integer>> queryVirtualLinksByPhysicalTopologyId(Integer id) {
        return physicalTopologyService.queryVirtualLinksByPhysicalTopologyId(id);
    }

    @ApiOperation(value = "导入拓扑文件", notes = "input的file标签实例：< type=file  name=file />,input的名称标签实例：< type=text  name=name />")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "xml文件"), @ApiImplicitParam(name = "name", value = "拓扑文件名称")})
    @RequestMapping({"import_xml"})
    @JSON
    public Result importXml(@RequestParam(name = "file") MultipartFile file, String name) {
        Result result = new Result();
        if (file == null || name == null) {
            result.setResult("参数不合法，为空");
            result.setSuccess(false);
            return result;
        }

        result.setResult("导入xml文件成功");
        try {
            InputStream is = file.getInputStream();
            physicalTopologyService.importXml(is, name);
        } catch (DocumentException e) {
            result.setResult("xml文件格式不正确");
            result.setSuccess(false);
            return result;
        } catch (IOException e) {
            result.setResult("读取xml错误" + e.toString());
            result.setSuccess(false);
            return result;
        }
        return result;
    }

}
